package com.yun.zf.app.controller;

import com.cxytiandi.encrypt.springboot.annotation.Encrypt;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.yun.zf.app.service.ZfFollowService;
import com.yun.zf.common.annotation.LogPrint;
import com.yun.zf.common.app.model.home.FollowMo;
import com.yun.zf.common.vo.Response;
import com.yun.zf.system.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags = "关注")
@RequestMapping("/follow")
public class FollowController extends BasicController {
    @Autowired
    private ZfFollowService followService;

    @ApiOperation(value = "生成加密字符串")
    @PostMapping("/getEncryptStr")
    @Encrypt
    public String getEncryptStr(String context) {
        return context;
    }

    @ApiOperation(value = "关注房源")
    @PostMapping("/houseFollow")
    @LogPrint(description = "关注房源")
    public Response houseFollow(@RequestBody FollowMo followMo) {
        int count = followService.addFollow(followMo);
        if (count > 0) {
            return Response.success("关注成功");
        }
        return Response.error("关注失败");
    }

    /**
     * userID 处理，在登录后将ID 给到前台
     *
     * @return
     */
    @ApiOperation(value = "我的关注")
    @PostMapping("/myFollow")
    @DynamicParameters(name = "SendOrderMapModel",properties = {
            @DynamicParameter(name = "type",value = "关注类型",example = "1",required = true)
    })
    @LogPrint(description = "我的关注")
    public Response myFollow(@RequestBody Map<String, String> map) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        return Response.success(followService.myFollow(userId, Integer.parseInt(map.get("type"))));
    }

    @ApiOperation(value = "移除关注")
    @PostMapping("/delFollow")
    @DynamicParameters(name = "SendOrderMapModel",properties = {
            @DynamicParameter(name = "type",value = "关注类型",example = "1",required = true),
            @DynamicParameter(name = "horcIds",value = "id",example = "1",required = true)
    })
    @LogPrint(description = "移除关注")
    public Response delFollow(@RequestBody Map<String, String> map) {
        String horcIds = map.get("horcIds");
        Long userId = SecurityUtils.getLoginUser().getUserId();
        List<Long> ids = Arrays.stream(horcIds.split(","))
                .map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        return Response.success(followService.delFollow(userId, ids, Integer.parseInt(map.get("type"))));
    }
}
